package cn.forward.template;

import cn.dev33.satoken.stp.StpInterface;
import cn.forward.properties.RedisKeyProperties;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

//@Component     保证此类被 SpringBoot 扫描，完成 Sa-Token 的自定义权限验证扩展
public class StpInterfaceImpl implements StpInterface {

    private RedisTemplate redisTemplate;


    private  RedisKeyProperties redisKeyProperties;

    public StpInterfaceImpl(RedisTemplate redisTemplate, RedisKeyProperties redisKeyProperties) {
        this.redisTemplate = redisTemplate;

        this.redisKeyProperties = redisKeyProperties;
    }

    /**
     * 返回一个账号所拥有的权限码集合 
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {

        Object permission = redisTemplate.opsForValue().get(redisKeyProperties.getRedisKey());

        JSONArray objects = JSONUtil.parseArray(permission);

        List<String> list = JSONUtil.toList(objects,String.class);

        return list;
    }

    /**
     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {

        return null;
    }

}